/**
* questionChart functions
* copyright@Catosoft.com reserved, 2016
* 2016.06.20, created by Eric.Wang
*/


/*
* get data
*/
var getQuestionAnswerData = function(){
	var data = {

	};
	var option = {
		url: '/draft/question/answer/v1',
		type: 'GET',
		data: data,
	};
	createAjax(option, function(err, res){
		if(!err){
			questionSourceColumnChart('questionSourceColumnChart', res);
			questionStatusColumnChart('questionStatusColumnChart', res);
			questionSourceTable('questionSourceTable', res);
			questionStatusTable('questionStatusTable', res);
			questionTimeTable('questionTimeTable', res);
		}
	});
}

var getQuestionDistributeData = function(){
	var data = {

	};
	var option = {
		url: '/draft/question/distribute/v1',
		type: 'GET',
		data: data,
	};
	createAjax(option, function(err, res){
		if(!err){
			questionDistributeColumnChart('questionDistributeColumnChart', res);
			questionDistributeTable('questionDistributeTable', res);
		}
	});
}

var getQuestionTopData = function(){
	var data = {

	};
	var option = {
		url: '/draft/question/top/v1',
		type: 'GET',
		data: data,
	};
	createAjax(option, function(err, res){
		if(!err){
			questionTopTable('questionTopTable', res);
		}
	});
}

var getQuestionStatData = function(){
	var data = {

	};
	var option = {
		url: '/draft/question/stat/v1',
		type: 'GET',
		data: data,
	};
	createAjax(option, function(err, res){
		if(!err){
			questionStatColumnChart('questionStatColumnChart', res);
			questionStatTable('questionStatTable', res);
		}
	});
}

var formatStatData = function (data) {
	var rows = [];
	data.list.forEach(function (item) {
		var firstRow = {
			name: item.pursuerName,
			ask: 0,
			answer: 0,
			close: 0,
			cancel: 0,
			total: 0
		};

		var otherRows = [];
		item.list.forEach(function (obj) {
			var name = obj.name;
			var ask = obj.ask_count;
			var answer = obj.answer_count;
			var close = obj.close_count;
			var cancel = obj.cancel_count;
			var total = obj.total;

			firstRow.ask += ask;
			firstRow.answer += answer;
			firstRow.close += close;
			firstRow.cancel += cancel;
			firstRow.total += total;

			otherRows.push({
				name: name,
				ask: ask,
				answer: answer,
				close: close,
				cancel: cancel,
				total: total
			});
		});

		rows.push(firstRow);
		// Array.prototype.push.apply(rows, otherRows);
		otherRows.forEach(function (row) {
			rows.push(row);
		});
	});

	return rows;
};

/*
* data filter
*/
var questionSourceColumnFilter = function(data){
	var series = [{
		name: '系统疑问数',
		identifier: 'sys_num',
		data: [],
	}, {
		name: 'DM疑问数',
		identifier: 'dm_num',
		data: [],
	}, {
		name: 'CRA疑问数',
		identifier: 'cra_num',
		data: [],
	}, {
		name: 'MR疑问数',
		identifier: 'mr_num',
		data: [],
	}];
	var categories = [];
	columnFilter(data, series, categories);
	return {
		series: series,
		categories: categories,
	};
}

var questionStatusColumnFilter = function(data){
	var series = [{
		name: '已解答疑问',
		identifier: 'done',
		data: [],
	}, {
		name: '未解答疑问',
		identifier: 'not',
		data: [],
	}];
	var categories = [];
	columnFilter(data, series, categories);
	return {
		series: series,
		categories: categories,
	};
}

var questionDistributeColumnFilter = function(data){
	var series = [{
		name: '疑问数量',
		identifier: 'num',
		data: [],
	}];
	var categories = [];
	columnFilter(data, series, categories);
	return {
		series: series,
		categories: categories,
	};
}

/*
* draw charts
*/
var questionSourceColumnChart = function(element, data){
	var filter = questionSourceColumnFilter(data.list);
	var questionSourceColumnChartOption = {
		chart: {
	        type: 'column',
	        renderTo: element,
	        zoomType: 'x'
	    },
	    colors: colors[0],
		title: {
	        text: chartTitles.questionSourceColumn,
	    },
	    xAxis: {
	    	title: {
	    		text: '中心',
	    	},
	    	type: 'category',
	    	categories: filter.categories,
	    },
	    yAxis: {
	        title: {
	            text: '数量',
	        },
	        plotLines: [{
	            value: 0,
	            width: 1,
	            color: '#808080'
	        }]
	    },
	    legend: {
	        layout: 'vertical',
	        align: 'right',
	        verticalAlign: 'middle',
	        borderWidth: 0
	    },
	    series: filter.series,
	    credits: credits,
	    exporting: {
	    	buttons: {
	    		contextButton: {
	    			menuItems: getMenuItems(),
	    		}
	    	}
	    },
	};
	var chart = new Highcharts.Chart(questionSourceColumnChartOption);
	bindSelect(element, chart, filter);
}

var questionStatusColumnChart = function(element, data){
	var filter = questionStatusColumnFilter(data.list);
	var questionStatusColumnOption = {
		chart: {
	        type: 'column',
	        renderTo: element,
	        zoomType: 'x'
	    },
	    colors: colors[0],
		title: {
	        text: chartTitles.questionStatusColumn,
	    },
	    xAxis: {
	    	title: {
	    		text: '中心',
	    	},
	    	type: 'category',
	    	categories: filter.categories,
	    },
	    yAxis: {
	        title: {
	            text: '数量',
	        },
	        plotLines: [{
	            value: 0,
	            width: 1,
	            color: '#808080'
	        }]
	    },
	    legend: {
	        layout: 'vertical',
	        align: 'right',
	        verticalAlign: 'middle',
	        borderWidth: 0
	    },
	    series: filter.series,
	    credits: credits,
	    exporting: {
	    	buttons: {
	    		contextButton: {
	    			menuItems: getMenuItems(),
	    		}
	    	}
	    },
	};
	var chart = new Highcharts.Chart(questionStatusColumnOption);
	bindSelect(element, chart, filter);
}

var questionDistributeColumnChart = function(element, data){
	var filter = questionDistributeColumnFilter(data.list);
	var questionDistributeColumnOption = {
		chart: {
	        type: 'column',
	        renderTo: element,
	        zoomType: 'x'
	    },
	    colors: colors[0],
		title: {
	        text: chartTitles.questionDistributeColumn,
	    },
	    xAxis: {
	    	type: 'category',
	    	categories: filter.categories,
	    },
	    yAxis: {
	        title: {
	            text: '数量',
	        },
	        plotLines: [{
	            value: 0,
	            width: 1,
	            color: '#808080'
	        }]
	    },
	    legend: {
	        layout: 'vertical',
	        align: 'right',
	        verticalAlign: 'middle',
	        borderWidth: 0
	    },
	    series: filter.series,
	    credits: credits,
	    exporting: {
	    	buttons: {
	    		contextButton: {
	    			menuItems: getMenuItems(),
	    		}
	    	}
	    },
	};
	var chart = new Highcharts.Chart(questionDistributeColumnOption);
	bindSelect(element, chart, filter);
}

var questionStatColumnFilter = function (data) {
	var categories = [];
  var askData = [];
  var answerData = [];
  var closeData = [];
  var cancelData = [];

	data.forEach(function (item) {
		categories.push(item.pursuerName);

		var ask = 0;
		var answer = 0;
		var close = 0;
		var cancel = 0;
		item.list.forEach(function (obj, index) {
			ask += obj.ask_count;
			answer += obj.answer_count;
			close += obj.close_count;
			cancel += obj.cancel_count;
		});
		askData.push(ask);
		answerData.push(answer);
		closeData.push(close);
		cancelData.push(cancel);
	});

	var series = [{
      name: '撤销',
      data: cancelData
  }, {
      name: '关闭',
      data: closeData
  }, {
      name: '回答',
      data: answerData
  }, {
      name: '发送',
      data: askData
  }];

  return {
  	categories: categories,
  	series: series
  };
};

var questionStatColumnChart = function (element, data) {
	var filter = questionStatColumnFilter(data.list);

	var questionStatColumnOption = {
		chart: {
	      type: 'column',
	      renderTo: element,
	      zoomType: 'x'
	  },
	  title: {
	      text: '疑问总体情况-柱状图'
	  },
	  xAxis: {
	      categories: filter.categories
	  },
	  yAxis: {
	      min: 0,
	      title: {
	          text: ''
	      }
	  },
	  tooltip: {
	      pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.percentage:.0f})<br/>',
	      shared: true
	  },
    plotOptions: {
	    column: {
	        stacking: 'normal'
	    }
    },
	  series: filter.series
	};

	var chart = new Highcharts.Chart(questionStatColumnOption);
	bindSelect(element, chart, filter);
}

var questionSourceTable = function(element, data){
	$('#' + element).bootstrapTable({
		data: data.list,
		classes: 'table table-hover',
		pagination: false,
		pageSize: 20,
		pageNumber: 1,
		pageList: [20, 50],
		sidePagination: 'client',
		showRefresh: true,
		showColumns: true,
		showFooter: true,
		buttonsAlign: 'right',
		showExport: true,
		exportDataType: 'all',
		exportTypes: ['csv', 'txt', 'excel'],
		locale: 'zh-CN',
		columns: [
		{
			field: 'Number',  
	    title: '序号',  
	    formatter: function (value, row, index) {  
	    	return index + 1;  
	    },
	    footerFormatter: function () {
	    	return '总计';
	    }
		},
		{
			title: '中心',
			field: 'name',
		}, {
			title: '系统疑问数',
			field: 'sys_num',
			footerFormatter: sumFormatter
		}, {
			title: 'DM疑问数',
			field: 'dm_num',
			footerFormatter: sumFormatter
		}, {
			title: 'CRA疑问数',
			field: 'cra_num',
			footerFormatter: sumFormatter
		}, {
			title: 'MR疑问数',
			field: 'mr_num',
			footerFormatter: sumFormatter
		}, {
			title: '疑问总数',
			field: 'count',
			footerFormatter: sumFormatter
		}],

	});
}

var questionStatusTable = function(element, data){
	$('#' + element).bootstrapTable({
		data: data.list,
		classes: 'table table-hover',
		pagination: false,
		pageSize: 20,
		pageNumber: 1,
		pageList: [20, 50],
		sidePagination: 'client',
		showRefresh: true,
		showColumns: true,
		showFooter: true,
		buttonsAlign: 'right',
		showExport: true,
		exportDataType: 'all',
		exportTypes: ['csv', 'txt', 'excel'],
		locale: 'zh-CN',
		columns: [
		{
			field: 'Number',  
	    title: '序号',  
	    formatter: function (value, row, index) {  
	    	return index + 1;  
	    },
	    footerFormatter: function () {
	    	return '总计';
	    }
		},
		{
			title: '中心',
			field: 'name',
		}, {
			title: '已解答疑问',
			field: 'done',
			footerFormatter: sumFormatter
		}, {
			title: '未解答疑问',
			field: 'not',
			footerFormatter: sumFormatter
		}, {
			title: '解答率',
			field: 'rate',
			footerFormatter: sumFormatter
		}],

	});
}

var questionTimeTable = function(element, data){
	$('#' + element).bootstrapTable({
		data: data.list,
		classes: 'table table-hover',
		pagination: false,
		pageSize: 20,
		pageNumber: 1,
		pageList: [20, 50],
		sidePagination: 'client',
		showRefresh: true,
		showColumns: true,
		showFooter: true,
		buttonsAlign: 'right',
		showExport: true,
		exportDataType: 'all',
		exportTypes: ['csv', 'txt', 'excel'],
		locale: 'zh-CN',
		columns: [
		{
			field: 'Number',  
	    title: '序号',  
	    formatter: function (value, row, index) {  
	    	return index + 1;  
	    },
	    footerFormatter: function () {
	    	return '总计';
	    }
		},
		{
			title: '中心',
			field: 'name',
		}, {
			title: '疑问最短解答时间（天）',
			field: 'short_time',
			footerFormatter: sumFormatter
		}, {
			title: '疑问最长解答时间（天）',
			field: 'long_time',
			footerFormatter: sumFormatter
		}, {
			title: '疑问平均解答时间（天）',
			field: 'avg_time',
			footerFormatter: sumFormatter
		}, {
			title: '疑问中位解答时间（天）',
			field: 'mid_time',
			footerFormatter: sumFormatter
		}],

	});
}

var questionDistributeTable = function(element, data){
	$('#' + element).bootstrapTable({
		data: data.list,
		classes: 'table table-hover',
		pagination: false,
		pageSize: 20,
		pageNumber: 1,
		pageList: [20, 50],
		sidePagination: 'client',
		showRefresh: true,
		showColumns: true,
		showFooter: true,
		buttonsAlign: 'right',
		showExport: true,
		exportDataType: 'all',
		exportTypes: ['csv', 'txt', 'excel'],
		locale: 'zh-CN',
		columns: [
		{
			field: 'Number',  
	    title: '序号',  
	    formatter: function (value, row, index) {  
	    	return index + 1;  
	    },
	    footerFormatter: function () {
	    	return '总计';
	    }
		},
		{
			title: '疑问类型',
			field: 'name',
		}, {
			title: '疑问数量',
			field: 'num',
			footerFormatter: sumFormatter
		}],

	});
}

var questionTopTable = function(element, data){
	$('#' + element).bootstrapTable({
		data: data.list,
		classes: 'table table-hover',
		pagination: false,
		pageSize: 20,
		pageNumber: 1,
		pageList: [20, 50],
		sidePagination: 'client',
		showRefresh: true,
		showColumns: true,
		showFooter: true,
		buttonsAlign: 'right',
		showExport: true,
		exportDataType: 'all',
		exportTypes: ['csv', 'txt', 'excel'],
		locale: 'zh-CN',
		columns: [
		{
			field: 'Number',  
	    title: '序号',  
	    formatter: function (value, row, index) {  
	    	return index + 1;  
	    },
	    footerFormatter: function () {
	    	return '总计';
	    }
		},
		{
			title: '疑问内容',
			field: 'name',
		}, {
			title: '疑问次数',
			field: 'num',
			footerFormatter: sumFormatter
		}],

	});
}

var questionStatTable = function(element, data){
	var rows = formatStatData(data);
	$('#' + element).bootstrapTable({
		data: rows,
		classes: 'table table-hover',
		pagination: false,
		pageSize: 20,
		pageNumber: 1,
		pageList: [20, 50],
		sidePagination: 'client',
		showRefresh: true,
		showColumns: true,
		showFooter: true,
		buttonsAlign: 'right',
		showExport: true,
		exportDataType: 'all',
		exportTypes: ['csv', 'txt', 'excel'],
		locale: 'zh-CN',
		columns: [
		/*{
			field: 'Number',  
	    title: '序号',  
	    formatter: function (value, row, index) {  
	    	return index + 1;  
	    },
	    footerFormatter: function () {
	    	return '总计';
	    }
		},*/
		{
			title: '中心',
			field: 'name',
		},
		{
			title: '发送',
			field: 'ask',
			footerFormatter: sumFormatter
		},
		{
			title: '回答',
			field: 'answer',
			footerFormatter: sumFormatter
		},
		{
			title: '关闭',
			field: 'close',
			footerFormatter: sumFormatter
		},
		{
			title: '撤销',
			field: 'cancel',
			footerFormatter: sumFormatter
		},
		{
			title: '总计',
			field: 'total',
			footerFormatter: sumFormatter
		}
		],
	});
}


$(function(){
	getQuestionAnswerData();
	getQuestionDistributeData();
	getQuestionTopData();
	getQuestionStatData();
});